﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;

using W = WorkNodeFramework.WorkNodeTable;
using System.ComponentModel;
namespace WorkNodeFramework
{

    public abstract class WorkNode:FieldSet<WorkNode>
    {
        public WorkNode() { }

        WorkPart workPart;
        internal WorkPart WorkPart {
            get {
                return workPart;
            }
            set {
                workPart = value;
            }
        }

        string fullPath;
        [Browsable(false)]
        public string FullPath {
            get {
                return fullPath;
            }
            internal set {
                fullPath = value;
            }
        }
        /// <summary>
        /// Gets id of WorkNode
        /// </summary>
        public Guid ID
        {
            get
            {
                object id = base[W.IDColumnName];
                if (id == null)
                    return Guid.Empty;
                return (Guid)id;
            }
            set
            {
                if (ID != value)
                {
                    base[W.IDColumnName] = value;
                    //=================
                    foreach (WorkNodeProperties p in Properties)
                        p.WorkNodeID = value;
                    foreach (WorkNodeLangProperties lp in LangProperties)
                        lp.WorkNodeID = value;
                }
            }
        }
        /// <summary>
        /// Gets or sets name of WorkNode
        /// </summary>
        public string Name { 
            get {
                return (string)base[W.NameColumnName];
            }
            set {
                base[W.NameColumnName] = value;
            }
        }
        /// <summary>
        /// Gets path of WorkNode in the storage
        /// </summary>
        [Browsable(false)]
        public string Path {
            get {
                return (string)base[W.PathColumnName];
            }
            internal set {
                base[W.PathColumnName] = value;
            }
        }
        /// <summary>
        /// Gets the parent id of WorkNode
        /// </summary>
        public Guid? ParentID {
            get {
                return (Guid?)base[W.ParentIDColumnName];
            }
            internal set {
                base[W.ParentIDColumnName] = value;
            }
        }
        /// <summary>
        /// Gets or sets type name of WorkNode
        /// </summary>
        [Browsable(false)]
        public string WorkNodeType {
            get {
                return (string)base[W.TypeColumnName];
            }
            set {
                base[W.TypeColumnName] = value;
            }
        }
        /// <summary>
        /// Gets or sets attributes of WorkNode
        /// </summary>
        [Browsable(false)]
        public int Attributes {
            get {
                return (int)base[W.AtrributesColumnName];
            }
            set {
                base[W.AtrributesColumnName] = value;
            }
        }

        /// <summary>
        /// Gets open date of WorkNode
        /// </summary>
        public DateTime OpenDate
        {
            get
            {
                if (IsNew)
                    return DateTime.Now;
                return (DateTime)base[W.OpenDateColumnName];
            }
            set
            {
                base[W.OpenDateColumnName] = value;
            }
        }

        /// <summary>
        /// Gets or sets last modified date of WorkNode
        /// </summary>
        public DateTime? LastDate
        {
            get
            {
                return (DateTime?)base[W.LastDateColumnName];
            }
            set
            {
                base[W.LastDateColumnName] = value;
            }
        }

        string langID =null;
        /// <summary>
        /// Gets or sets a language id for WorkNode
        /// </summary>
        [Browsable(false)]
        public string LangID {
            get {
                if (langID == null)
                    return Thread.CurrentThread.CurrentUICulture.Name;
                return langID;
            }
            set {
                langID = value;
            }
        }

        internal DataProvider Provider {
            get {
                if (workPart != null)
                    return workPart.Provider;
                return null;
            }
        }

        WorkNodePropertyCollection<WorkNodeProperties> properties;
        /// <summary>
        /// Gets the none culture properties of WorkNode
        /// </summary>
        internal protected virtual WorkNodePropertyCollection<WorkNodeProperties> Properties
        {
            get
            {
                if (properties == null)
                    properties = new WorkNodePropertyCollection<WorkNodeProperties>(this);
                return properties;
            }
        }
        WorkNodeLangPropertyCollection<WorkNodeLangProperties> langProperties;
        /// <summary>
        /// Gets the culture properties of WorkNode
        /// </summary>
        internal protected virtual WorkNodeLangPropertyCollection<WorkNodeLangProperties> LangProperties
        {
            get {
                if (langProperties == null)
                    langProperties = new WorkNodeLangPropertyCollection<WorkNodeLangProperties>(this, LangID);
                return langProperties;
            }
        }

        public new string this[string Name] {
            get {
                return Properties[Name];
            }
            set {
                Properties[Name] = value;
            }
        }

        public string this[string Name, string LangID] {
            get {
                return LangProperties[Name, LangID];
            }
            set {
                LangProperties[Name, LangID] = value;
            }
        }

        public void Set(string Name, object Value) {
            Properties.Set(Name, Value);
        }

        public T Get<T>(string Name) {
            return Get<T>(Name, default(T));
        }

        public T Get<T>(string Name, T DefaultValue) {
            return Properties.Get<T>(Name, DefaultValue);
        }

        public void Set(string Name, string LangID, object Value) {
            LangProperties.Set(Name, LangID, Value);
        }

        public T Get<T>(string Name, string LangID) {
            return Get<T>(Name, LangID, default(T));
        }

        public T Get<T>(string Name, string LangID, T DefaultValue) {
            return LangProperties.Get<T>(Name, LangID, DefaultValue);
        }

        internal override void Save(ITable table)
        {
            //Nothing
        }

        public override string ToString()
        {
            if(!string.IsNullOrEmpty(Name))
                return Name;
            return base.ToString();
        }
    }
}
 